/**
* 用户详情对话框（可用于添加和修改对话框）
*/
var SchoolInfoDlg = {
    width: '650px',
    height: 'auto',
    currentLayerIndex: -1,
    cache: "",
    InfoData: {}
};

/**
 * 初始化新增弹出框
 */
SchoolInfoDlg.init = function () {
    this.cache = $("#schoolDialog").html();
    $("#schoolDialog").html('');
};

/**
 * 清除数据
 */
SchoolInfoDlg.clearData = function () {
    this.InfoData = {};
};

/**
 * 关闭此对话框
 */
SchoolInfoDlg.close = function () {
    layer.close(this.currentLayerIndex);
};

/**
 * 新增打开模态框调用事件
 */
SchoolInfoDlg.renderAdd = function () {
    //初始化icheck选中样式
    initICheck();
    //初始化
    SchoolInfoDlg.provinceList();
};
/**
 * 新增
 */
SchoolInfoDlg.addSubmit = function (index) {

    this.clearData();
    this.InfoData = getFormJson("#frmMain");
    if(this.InfoData.password==="") this.InfoData.password=null;
    // 提交信息
    var ajax = new $ax(Feng.ctxPath+"/school/save", function (data) {
        if (data.success === true) {
            Feng.success("添加成功!");
            layer.close(index);
            SchoolTable.me.refresh();
        } else if (data.code === 2) {
            Feng.error(data.msg);
        } else {
            Feng.error("添加失败!");
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.InfoData);
    ajax.start();
};

/**
 * 编辑
 */
SchoolInfoDlg.load = function (id) {
    var ajax = new $ax(Feng.ctxPath+"/school/getById", function(data){
        Feng.log("学校信息查询成功");
        if (data.success){
            for (var i in data.obj){
                //判断Id与name是否相同
                if($("#"+i).attr("name")==i){
                    $("#"+i).val(data.obj[i]);
                }
            }
            selectProvince();

            $("#cityId").val(data.obj.cityId);
        }
    },function(data){
        Feng.error("加载信息失败!");
    });
    ajax.data={id:id};
    ajax.start();
};


/**
 * 提交修改
 */
SchoolInfoDlg.editSubmit = function () {

    this.clearData();
    this.InfoData = getFormJson("#frmMain");
    if(this.InfoData.password==="") this.InfoData.password=null;

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/school/update", function (data) {
        Feng.success("修改成功!");
        SchoolTable.me.refresh();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.InfoData);
    ajax.start();
};


//初始化省列表
SchoolInfoDlg.provinceList = function () {
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/province/getProvinceList", function (data) {
        Feng.log("查询省列表成功");
        var select = "<option value='' disabled selected>--请选择省--</option>";
        data=data.obj||[];
        if(data.length>0){
            for(var i=0,len=data.length; i<len; i++){
                select+="<option value='{0}'>{1}</option>".format(data[i].id,data[i].name);
            }
            $("#provinceId").html(select);
        }
    }, function (data) {
        Feng.error("省列表查询失败!");
    });
    ajax.start();
};

//获取市
function selectProvince() {
    var provinceId = $("#provinceId").val();
    var ajax = new $ax(Feng.ctxPath + "/province/selectLocByProvinceId", function(data){
        Feng.log("查询市列表成功");
        var select = "<option value='' disabled selected>--请选择市--</option>";
        data=data.obj||[];
        if(data.length>0){
            for(var i=0,len=data.length; i<len; i++){
                select+="<option value='{0}'>{1}</option>".format(data[i].id,data[i].city);
            }
            $("#cityId").html(select);
        }
    },function(data){
        Feng.error("加载信息失败!");
    });
    ajax.data={provinceId:provinceId};
    ajax.start();
}




